//
//  ByCellPhysicalFixed.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/14.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByCellPhysicalFixed.h"

// vc
#import "ByPhysicalProductDetailVC.h"

@implementation ByCellPhysicalFixed
{
    UIImageView *_img;
    UILabel     *_titleLabel;
    UILabel     *_subLabel;
    UILabel     *_priceLabel;
    UILabel     *_originLabel;
    UIButton    *_lookButton;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByCellPhysicalFixed";
    ByCellPhysicalFixed *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        [self createViews];
    }
    return self;
}


- (void)createViews{
    _img  = [[UIImageView alloc] init];
    [self addSubview:_img];
    
    _titleLabel = [[UILabel alloc] init];
    [self addSubview:_titleLabel];
    _titleLabel.font = FONT(14);
    
    _subLabel = [[UILabel alloc] init];
    [self addSubview:_subLabel];
    _subLabel.font = FONT(13);
    _subLabel.textColor = [UIColor lightGrayColor];
    
    _priceLabel = [[UILabel alloc] init];
    [self addSubview:_priceLabel];
    _priceLabel.font = FONT(14);
    _priceLabel.textColor = BY_THEME_COLOR;
    
    _originLabel = [[UILabel alloc] init];
    [self addSubview:_originLabel];
    _originLabel.font = FONT(13);
    
    _lookButton = [[UIButton alloc] init];
    [self addSubview:_lookButton];
    [_lookButton setTitle:@"查看详情" forState:UIControlStateNormal];
    [_lookButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_lookButton setBackgroundColor:BY_THEME_COLOR];
    _lookButton.titleLabel.font = FONT(14);
    _lookButton.layer.cornerRadius = 4.0;
    
}

- (void)setModel:(ByModelPhysicalFixed *)model{
    _model = model;
    
    [_img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(70, 70));
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_img.mas_right).offset(15);
        make.top.equalTo(@(15));
        make.right.equalTo(@(-10));
        make.height.equalTo(@(20));
    }];
    
    CGFloat width = [@"查看详情" getSizeOfString:14].width + 20;
    [_lookButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-10));
        make.top.equalTo(_titleLabel.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(width, 25));
    }];
    
    [_subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel.mas_left);
        make.top.equalTo(_titleLabel.mas_bottom).offset(5);
        make.height.equalTo(@(15));
        make.right.equalTo(_lookButton.mas_left);
    }];
    
    width = [_model.price getSizeOfString:14].width;
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_subLabel.mas_bottom).offset(5);
        make.left.equalTo(_titleLabel.mas_left);
        make.size.mas_equalTo(CGSizeMake(width, 15));
    }];
    
    [_originLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_priceLabel.mas_right).offset(5);
        make.top.equalTo(_priceLabel.mas_top);
        make.right.equalTo(@(-10));
        make.height.equalTo(@(15));
    }];
    
    
    // 测试数据
    _img.backgroundColor = BY_THEME_COLOR;
    _titleLabel.text = _model.title;
    _subLabel.text = _model.subtitle;
    _priceLabel.text = _model.price;
    _originLabel.text = _model.originPrice;
    
    [_lookButton addTarget:self action:@selector(lookAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)lookAction{
    ByPhysicalProductDetailVC *vc = [[ByPhysicalProductDetailVC alloc] init];
    [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
}

@end
